<template>
  <div>
    <el-dialog
      :title="'企业名称: ' + vipUidObjRole.userGroupName"
      :visible.sync="outerVisible"
      :close-on-click-modal="false"
      width="80%"
      append-to-body
      :before-close="handleClose"
      @close="close"
    >
      <div class="role-list">
        <el-form :inline="true" :model="vipRolePageForm" class="demo-form-inline">
          <el-form-item label="角色名称">
            <el-input v-model="vipRolePageForm.sysRoleName" placeholder="公司名称,支持模糊匹配" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="vipRolePageForm.sysRoleStatus" placeholder="请选择">
              <el-option label="请选择" value="" />
              <el-option label="已启用" value="0" />
              <el-option label="已禁用" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="warning" @click="roleAddInnerVisibleAdd">新增</el-button>
          </el-form-item>
        </el-form>
        <el-table :data="vipRolePageData.list" border style="width: 100%">
          <el-table-column label="角色名称" align="center">
            <template slot-scope="scope">
              {{ scope.row.sysRoleName }}
            </template>
          </el-table-column>
          <el-table-column label="角色类型" align="center">
            <template slot-scope="scope">
              <el-button v-if="scope.row.sysRoleType === '0'" type="danger" disabled>管理员</el-button>
              <el-button v-else-if="scope.row.sysRoleType === '1'" type="success" plain disabled>普通员工</el-button>
              <el-button v-else type="primary" plain disabled>其他</el-button>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-button v-if="scope.row.sysRoleStatus === '0'" type="success" disabled>已启用</el-button>
              <el-button v-else type="danger" plain disabled>已禁用</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <!--              <el-button size="medium " type="danger" @click="handleAuthor(scope.$index, scope.row)">授权</el-button>-->
              <el-button size="medium " type="primary" @click="handleRoleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button size="medium " type="warning" @click="handleStatus(scope.$index, scope.row)">状态</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            :current-page="vipRolePageData.pageNum"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="vipRolePageData.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="vipRolePageData.total"
            @size-change="handleRolePageSizeChange"
            @current-change="handleRolePageCurrentChange"
          />
        </div>
        <el-dialog
          width="30%"
          title="新增企业角色"
          :visible.sync="innerVisibleAdd"
          :close-on-click-modal="false"
          :before-close="handleVipRoleAddBeforeClose"
          append-to-body
          @close="handleVipRoleAddClose"
        >
          <vip-role-add :role-adduser-group-uuid="roleAdduserGroupUuid" @closeInnerVisibleAdd="closeInnerVisibleAdd" />
        </el-dialog>
        <el-dialog
          width="30%"
          title="编辑企业角色"
          :visible.sync="innerVisibleEdit"
          :close-on-click-modal="false"
          :before-close="handleVipRoleEditBeforeClose"
          append-to-body
          @close="handleVipRoleEditClose"
        >
          <vip-role-edit :role-eit-obj="roleEitObj" @closeInnerVisibleEdit="closeInnerVisibleEdit" />
        </el-dialog>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Vip from '@/api/axios/api/author/vip'
import Role from '@/api/axios/api/author/role'
import Tip from '@/utils/tip'
import VipRoleAdd from './VipRoleAdd'
import VipRoleEdit from './VipRoleEdit'
export default {
  name: 'VipRole',
  components: { VipRoleAdd, VipRoleEdit },
  props: {
    hideRoleCompBoolean: Boolean,
    vipUidObjRole: {}
  },
  data() {
    return {
      outerVisible: false,
      innerVisibleAdd: false,
      handleRoleEditBoolean: false,
      dialogVisibleAdd: false,
      innerVisibleEdit: false,
      roleAdduserGroupUuid: '',
      roleEditRoleUuid: '',
      roleEitObj: {},
      vipRolePageData: {},
      vipRolePageForm: {
        userGroupUuid: '',
        sysRoleName: '',
        sysRoleStatus: '',
        pageSize: 10,
        pageNum: 1
      }
    }
  },
  watch: {
    hideRoleCompBoolean: {
      handler() {
        this.outerVisible = this.hideRoleCompBoolean
        this.vipRolePageForm.userGroupUuid = this.vipUidObjRole.userGroupUuid
      }
    },
    vipUidObjRole: {
      handler() {
        this.vipRolePage()
      }
    }
  },
  methods: {
    close() {
      this.$emit('hideRoleChildComp', false)
    },
    handleVipRoleAddClose() {
      this.innerVisibleAdd = false
    },
    handleVipRoleEditClose() {
      this.innerVisibleEdit = false
    },
    handleVipRoleAddBeforeClose() {
      this.innerVisibleAdd = false
    },
    handleVipRoleEditBeforeClose() {
      this.innerVisibleEdit = false
    },
    roleAddInnerVisibleAdd() {
      this.innerVisibleAdd = true
      this.roleAdduserGroupUuid = this.vipRolePageForm.userGroupUuid
    },
    closeInnerVisibleAdd() {
      this.innerVisibleAdd = false
    },
    closeInnerVisibleEdit() {
      this.innerVisibleEdit = false
      this.vipRolePage()
    },
    handleClose(done) {
      this.$emit('hideRoleChildComp', false)
    },
    handleRolePageCurrentChange(pageNum) {
      this.vipRolePageForm.pageNum = pageNum
      this.vipRolePage()
    },
    handleRolePageSizeChange(pageSize) {
      this.vipRolePageForm.pageSize = pageSize
      this.vipRolePage()
    },
    handleRoleEdit(index, row) {
      Role.roleDetail(row).then(res => {
        this.innerVisibleEdit = true
        this.handleRoleEditBoolean = true
        this.roleEitObj = res.data
      })
    },
    handleStatus(index, row) {
      let msg = '启用'
      if (row.sysRoleStatus === '0') {
        msg = '禁用'
      }
      this.$confirm('您确定要 ' + msg + ' ' + row.sysRoleName + ' 吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Vip.vipRoleStatus(row).then(res => {
          if (res.status === 200) {
            this.$message({
              type: 'success',
              message: res.data
            })
            setTimeout(() => {
              this.vipRolePage()
            }, 1000)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消对 ' + row.sysRoleName + ' 的状态变更'
        })
      })
    },
    onSubmit() {
      this.vipRolePage()
    },
    vipRolePage() {
      Vip.vipRolePage(this.vipRolePageForm).then(res => {
        if (res.status === 200) {
          this.vipRolePageData = res.data
        } else {
          Tip.error(res.data)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
